package org.mil.bean.vo;

import org.mil.bean.bo.*;
import org.mil.bean.dom.GenericDocumentObject;
import org.mil.logic.WikiNameInfo;
import org.mil.shell.MediaInfoCLI;
import org.mil.util.TextUtil;

/**
 * FileInfo的值对象，负责为商务对象（BO）输出友好的字串值
 * @author HZK
 *
 */
public class FileInfoVO extends GenericDocumentObject{
	FileInfo bo;	
	private VideoTrack videoTrack;
	private GeneralTrack generalTrack;
	private AudioTrack audioTrack1;
	//private AudioTrack audioTrack2;
	private H264ConfigInfo h264ConfigInfo;
	private String x264_ref="";
	private String x264_subme="";
	private String x264_bframes="";
	private String x264_rc="";
	private String x264_me="";
	private String x264_bitrate="";
	private String x264_crf="";
	
	private String encapsuleFormat="";
	private String fileSize="";
	private String fileName="";
	private String duration="";
	private String encodeDate="";	
	
	
	private String v_format="";
	private String v_width="";
	private String v_height="";
	private String v_density="";
	private String v_displayRatio="";
	private String v_bitRate="";
	private String v_displayResolution="";

	
	private String a1_format="";
	private String a1_channels="";
	private String a1_bitRate="";
	private String a1_samplingRate="";
	private String a1_lang="";

	private String nm_AsianName="";
	private String nm_EnglishName="";		
	private String nm_year="";		//原作品发行年代 
	private String nm_source="";		//压制片源
	private String nm_version="";		//片源版本
	private String nm_producer="";	//压片作者或其组织
	private String nm_standard="";	//采用的压片标准
	public static FileInfoVO newInstance(FileInfo bo){
		if (bo==null) return null;
		FileInfoVO vo=new FileInfoVO();
		vo.load(bo);
		return vo;
	}
	public void load(FileInfo bo){
		if (bo==null) return;
		videoTrack=bo.getChiefVideoTrack();
		audioTrack1=bo.getChiefAudioTrack();
		generalTrack=bo.getGeneralTrack();
		//基本信息
		fileSize=TextUtil.get4FracStyle(bo.getFileSize());		
		fileName=bo.getFileName();
		
		if (generalTrack!=null){
			encapsuleFormat= generalTrack.getFormat();
			encodeDate=valueOf(TextUtil.formatTime(generalTrack.getEncoded_date()));
			duration=valueOf(generalTrack.getDuration());
		}
		
		//视频信息
		if (videoTrack!=null){
			v_format=videoTrack.getFormat();
			v_width=valueOf(videoTrack.getWidth());
			v_height=valueOf(videoTrack.getHeight());			 
			v_density=valueWith4FracOf(videoTrack.getBits_per_pixelframe_x10k()/10000.0);
			//若duration字段未在generalTrack中出现，用videoTrack中的字段代替
			if (duration==null || duration=="")
			duration=valueOf(videoTrack.getDuration());
			
			v_bitRate=valueOf(videoTrack.getNominal_bit_rate());
			
			//计算实际宽分辨率dw
			int rw=videoTrack.getDisplay_aspect_ratio_w();
			int rh=videoTrack.getDisplay_aspect_ratio_h();
			v_displayRatio=rw+":"+rh;
			
			float dw=videoTrack.getHeight()/(float)rh*rw;
			int resol=Math.round(dw/16f*9);
			//!!! tentative
			v_displayRatio=valueWith4FracOf(videoTrack.getDisplay_aspect_ratio_w()/(float)videoTrack.getDisplay_aspect_ratio_h());
			resol=Math.round(videoTrack.getWidth()/16f*9);
			v_displayResolution=resol+"p";
			
		}
		
		//临时方法：直接调用外部命令取得duration
		String fn=bo.getHandler().getAbsolutePath();
		String s=MediaInfoCLI.getSingleData(fn, "Video", "Duration");
		if (s!=null ) {
			int x=s.indexOf('\r');			
			if (x>0) {
				s=s.substring(0,x);
				if (s.length()>0){
				int ms=Integer.valueOf(s);
				double min=ms/60000f;
				duration=valueWith4FracOf(min);
				}
			}
		}

		
		// h264参数信息
		if (videoTrack!=null) h264ConfigInfo=videoTrack.getH264ConfigInfo();
		if (h264ConfigInfo!=null){
			x264_ref=valueOf(h264ConfigInfo.ref);
			x264_subme=valueOf(h264ConfigInfo.subme);
			x264_bframes=valueOf( h264ConfigInfo.bframes);
			x264_crf=valueWith2FracOf(h264ConfigInfo.crf_x100/100.0);
			x264_bitrate=valueOf(h264ConfigInfo.bitrate);
			x264_rc=getRc();	
			x264_me=getMe();
		}
		
		//音频轨道1信息
		if (audioTrack1!=null){
			a1_format=audioTrack1.getFormat();
			a1_channels=valueOf(audioTrack1.getNum_channels());
			a1_samplingRate=valueOf(audioTrack1.getSampling_rate());
			a1_bitRate=valueOf(audioTrack1.getBit_rate());
			//将title和language字段进行比较智能的合并
			String lang=audioTrack1.getLanguage();
			String title=audioTrack1.getTitle();			
			if (lang==null || lang=="") 
				a1_lang=valueOf(title);
			else {
				a1_lang=lang;
				if (title!=null && title!="" && !title.equals(lang)) 
					a1_lang=lang+"["+title+"]";
			}						
		}
		
		//wiki式命名分析
		WikiNameInfo wni=WikiNameInfo.newInstance(fileName);
		nm_AsianName=wni.getAsianName();
		nm_EnglishName=wni.getEnglishName();		
		nm_year=wni.getYear();		
		nm_source=wni.getSource();
		nm_version=wni.getVersion();
		nm_producer=wni.getProducer();
		nm_standard=wni.getStandard();
		
	}

  

	private String getRc(){		 
		switch (h264ConfigInfo.rc){
		case '2':return "2pass";
		case 'c':return "crf";
		case '3':return "3pass";
		default: return String.valueOf(h264ConfigInfo.rc);
		}	
	}
	private String getMe(){		 
		switch (h264ConfigInfo.me){
		case 'u':return "umh";
		case 'h':return "hex";
		case 'e':return "esa";
		 
		default: return String.valueOf(h264ConfigInfo.rc);
		}	
	}

	/**
	 * 重写String.valueOf方法。
	 * 用于自定义类型转换中，当数值为0时，输出空串
	 * @param arg
	 * @return
	 */
	protected String valueOf(byte arg){
		if (arg>0) return String.valueOf(arg);
		else return "";
	}
	protected String valueOf(int arg){
		if (arg>0) return String.valueOf(arg);
		else return "";
	}
	protected String valueOf(double arg){
		if (arg>0) return String.valueOf(arg);
		else return "";
	}
	protected String valueOf(String arg){
		if (arg==null ||arg=="0" ) return "";
		else return arg;
	}
	/**
	 * valueOf(byte arg)的加强版，可以输出指定位数的小数部分
	 * @param arg
	 * @return
	 */
	protected String valueWith2FracOf(double arg){
		if (arg>0) return String.valueOf(TextUtil.get2FracStyle(arg));
		else return "";
	}
	
	protected String valueWith4FracOf(double arg){
		if (arg>0) return String.valueOf(TextUtil.get4FracStyle(arg));
		else return "";
	}
	
	public String getX264_ref() {
		return x264_ref;
	}



	public String getX264_subme() {
		return x264_subme;
	}



	public String getX264_bframes() {
		return x264_bframes;
	}



	public String getX264_rc() {
		return x264_rc;
	}



	public String getX264_me() {
		return x264_me;
	}



	public String getX264_bitrate() {
		return x264_bitrate;
	}



	public String getX264_crf() {
		return x264_crf;
	}



	public String getEncapsuleFormat() {
		return encapsuleFormat;
	}



	public String getFileSize() {
		return fileSize;
	}



	public String getFileName() {
		return fileName;
	}
	public String getDuration() {
		return duration;
	}



	public String getEncodeDate() {
		return encodeDate;
	}



	public String getV_format() {
		return v_format;
	}



	public String getV_width() {
		return v_width;
	}



	public String getV_height() {
		return v_height;
	}



	public String getV_density() {
		return v_density;
	}



	public String getV_displayRatio() {
		return v_displayRatio;
	}
	public String getV_bitRate() {
		return v_bitRate;
	}
	public String getV_displayResolution() {
		return v_displayResolution;
	}
	public String getA1_format() {
		return a1_format;
	}



	public String getA1_channels() {
		return a1_channels;
	}

	public String getA1_bitRate() {
		return a1_bitRate;
	}
	public String getA1_samplingRate() {
		return a1_samplingRate;
	}
	public String getA1_lang() {
		return a1_lang;
	}


	//Tentative 
	public String getA1_ID(){
		if (audioTrack1==null) return "";
		return valueOf(audioTrack1.getID());
	}
	public String getNm_AsianName() {
		return nm_AsianName;
	}
	public String getNm_EnglishName() {
		return nm_EnglishName;
	}
	public String getNm_year() {
		return nm_year;
	}
	public String getNm_source() {
		return nm_source;
	}
	public String getNm_version() {
		return nm_version;
	}
	public String getNm_producer() {
		return nm_producer;
	}
	public String getNm_standard() {
		return nm_standard;
	}
	
	 
}
